home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / UID.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  144 lines

  1. /*
  2.  * @(#)UID.java    1.5 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi.server;
  15.  
  16. import java.io.*;
  17.  
  18. /**
  19.  * Abstraction for creating identifiers that are unique with respect
  20.  * to the the host on which it is generated.
  21.  */
  22. public final class UID implements java.io.Serializable {
  23.     private int unique;
  24.     private long time;
  25.     private short count;
  26.  
  27.     /**
  28.      * In the absence of an actual pid, just do something somewhat
  29.      * random.
  30.      */
  31.     private static int getHostUniqueNum() {
  32.     return (new Object()).hashCode();
  33.     }
  34.     
  35.     private static int hostUnique = getHostUniqueNum();
  36.     private static long lastTime = System.currentTimeMillis();
  37.     private static short lastCount = Short.MIN_VALUE;
  38.     private static Object mutex = new Object();
  39.     private static long  ONE_SECOND = 1000; // in milliseconds
  40.     
  41.     /**
  42.      * Create a pure identifier that is unique with respect to the
  43.      * host on which it is generated.  This UID is unique under the
  44.      * following conditions: a) the machine takes more than one second
  45.      * to reboot, and b) the machine's clock is never set backward.
  46.      * In order to construct a UID that is globally unique, simply
  47.      * pair a UID with an InetAddress.
  48.      */
  49.     public UID() {
  50.     
  51.     synchronized (mutex) {
  52.         if (lastCount == Short.MAX_VALUE) {
  53.         boolean done = false;
  54.         while (!done) {
  55.             time = System.currentTimeMillis();
  56.             if (time < lastTime+ONE_SECOND) {
  57.             // pause for a second to wait for time to change
  58.             try {
  59.                 Thread.currentThread().sleep(ONE_SECOND);
  60.             } catch (java.lang.InterruptedException e) {
  61.             }    // ignore exception
  62.             continue;
  63.             } else {
  64.             lastTime = time;
  65.             lastCount = Short.MIN_VALUE;
  66.             done = true;
  67.             }
  68.         }
  69.         } else {
  70.         time = lastTime;
  71.         }
  72.         unique = hostUnique;
  73.         count = lastCount++;
  74.     }
  75.     }
  76.  
  77.     /**
  78.      * Create a "well-known" ID.  There are 2^16 -1 such possible
  79.      * well-known ids.  An id generated via this constructor will not
  80.      * clash with any id generated via the default UID
  81.      * constructor which will generates a genuinely unique identifier
  82.      * with respect to this host.
  83.      */
  84.     public UID(short num) 
  85.     {
  86.     unique = 0;
  87.     time = 0;
  88.     count = num;
  89.     }
  90.  
  91.     private UID(int unique, long time, short count) 
  92.     {
  93.     this.unique = unique;
  94.     this.time = time;
  95.     this.count = count;
  96.     }
  97.  
  98.     public int hashCode() {
  99.     return (int)time + (int)count;
  100.     }
  101.  
  102.     public boolean equals(Object obj) {
  103.     if ((obj != null) && (obj instanceof UID)) {
  104.         UID uid = (UID)obj;
  105.         return (unique == uid.unique &&
  106.             count == uid.count &&
  107.             time == uid.time);
  108.     } else {
  109.         return false;
  110.     }
  111.     }
  112.     
  113.     public String toString() {
  114.     return Integer.toString(unique,16) + ":" +
  115.         Long.toString(time,16) + ":" +
  116.         Integer.toString(count,16);
  117.     }
  118.     
  119.     /**
  120.      * Write uid to output stream.
  121.      */
  122.     public void write(DataOutput out) throws java.io.IOException
  123.     {
  124.     out.writeInt(unique);
  125.     out.writeLong(time);
  126.     out.writeShort(count);
  127.     }
  128.     
  129.     /**
  130.      * Get the uid from the input stream.
  131.      * @param in the input stream
  132.      * @exception IOException If uid could not be read
  133.      * (due to stream failure or malformed uid)
  134.      */
  135.     public static UID read(DataInput in)
  136.     throws java.io.IOException
  137.     {
  138.     int unique = in.readInt();
  139.     long time = in.readLong();
  140.     short count = in.readShort();
  141.     return new UID(unique, time, count);
  142.     }
  143. }
  144.